﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace CDHunter.Manager
{
    class XMLCompareManager
    {
        /// <summary>
        /// Generate XML Compare Reports
        /// </summary>
        /// <param name="basicCS02XMLFilePath"></param>
        /// <param name="compareReportOutPath"></param>
        public static void GenerateXMLCompareReports(string basicCS02XMLFilePath, string compareReportOutPath)
        {
            string comparedCS07XMLFilePath = basicCS02XMLFilePath.Replace("_CS02", "_CS07");

            Console.WriteLine("Comparing {0} and {1}", basicCS02XMLFilePath, comparedCS07XMLFilePath);

            StreamReader readerCS02 = new StreamReader(basicCS02XMLFilePath, Encoding.ASCII);
            List<ProductInfo> CS02ProductInfoList = SerializerManager.Deserialize(typeof(List<ProductInfo>), readerCS02.ReadToEnd()) as List<ProductInfo>;
            readerCS02.Close();

            StreamReader readerCS07 = new StreamReader(comparedCS07XMLFilePath, Encoding.ASCII);
            List<ProductInfo> CS07ProductInfoList = SerializerManager.Deserialize(typeof(List<ProductInfo>), readerCS07.ReadToEnd()) as List<ProductInfo>;
            readerCS07.Close();

            List<ProductInfo> CS02specific = new List<ProductInfo>();
            List<ProductInfo> CS07specific = new List<ProductInfo>();
            Dictionary<ProductInfo, ProductInfo> CS02CS07difference = new Dictionary<ProductInfo, ProductInfo>();

            //Set CS02CS07difference and CS02specific
            foreach (ProductInfo CS02ProInfo in CS02ProductInfoList)
            {
                int CS07Index = CS07ProductInfoList.IndexOf(CS02ProInfo);

                if (CS07Index >= 0)
                {
                    if (!CS07ProductInfoList[CS07Index].ProductTitle.Equals(CS02ProInfo.ProductTitle))
                    {
                        CS02CS07difference.Add(CS02ProInfo, CS07ProductInfoList[CS07Index]);
                    }
                }
                else
                {
                    CS02specific.Add(CS02ProInfo);
                }
            }

            //Set CS07specific
            foreach (ProductInfo CS07ProInfo in CS07ProductInfoList)
            {
                int CS02Index = CS02ProductInfoList.IndexOf(CS07ProInfo);

                if (CS02Index < 0)
                {
                    CS07specific.Add(CS07ProInfo);
                }
            }

            //Output Compare Report
            StringBuilder sbCompareReportContent = new StringBuilder();

            if (CS02CS07difference != null && CS02CS07difference.Count > 0)
            {
                sbCompareReportContent.AppendLine("*********************** CS02/CS07 Differences ************************");
                foreach (KeyValuePair<ProductInfo, ProductInfo> pair in CS02CS07difference)
                {
                    sbCompareReportContent.Append("CS02---");
                    sbCompareReportContent.Append("ProductID: " + pair.Key.ProductID);
                    sbCompareReportContent.Append(", ProductTitle: " + pair.Key.ProductTitle);
                    sbCompareReportContent.AppendLine();

                    sbCompareReportContent.Append("CS07---");
                    sbCompareReportContent.Append("ProductID: " + pair.Value.ProductID);
                    sbCompareReportContent.Append(", ProductTitle: " + pair.Value.ProductTitle);

                    sbCompareReportContent.AppendLine();
                    sbCompareReportContent.AppendLine();
                }
            }

            if (CS02specific != null && CS02specific.Count > 0)
            {
                sbCompareReportContent.AppendLine("*********************** CS02 Specific ************************");
                foreach (ProductInfo pInfo02 in CS02specific)
                {
                    sbCompareReportContent.Append("ProductID: " + pInfo02.ProductID);
                    sbCompareReportContent.Append(", ProductTitle: " + pInfo02.ProductTitle);
                    sbCompareReportContent.AppendLine();
                }
                sbCompareReportContent.AppendLine();
            }

            if (CS07specific != null && CS07specific.Count > 0)
            {
                sbCompareReportContent.AppendLine("*********************** CS07 Specific ************************");
                foreach (ProductInfo pInfo07 in CS07specific)
                {
                    sbCompareReportContent.Append("ProductID: " + pInfo07.ProductID);
                    sbCompareReportContent.Append(", ProductTitle: " + pInfo07.ProductTitle);
                    sbCompareReportContent.AppendLine();
                }
                sbCompareReportContent.AppendLine();
            }

            if (String.IsNullOrEmpty(sbCompareReportContent.ToString().Trim()))
                sbCompareReportContent.AppendLine("There is no differences between CS02 and CS07.");

            StreamWriter writer = new StreamWriter(compareReportOutPath);
            writer.WriteLine(sbCompareReportContent.ToString());
            writer.Close();

        }

        /// <summary>
        /// Generate XML Compare Reports
        /// </summary>
        /// <param name="outPathList"></param>
        public static void GenerateXMLCompareReports(List<String> outPathList)
        {
            foreach (String outPath in outPathList)
            {
                DirectoryInfo directoryInfo = new DirectoryInfo(outPath);

                foreach (FileInfo fileInfo in directoryInfo.GetFiles())
                {
                    if (fileInfo.FullName.Contains("_CS02") && File.Exists(fileInfo.FullName.Replace("_CS02", "_CS07")))
                    {
                        string basicCS02XMLFilePath = fileInfo.FullName;

                        GenerateXMLCompareReports(basicCS02XMLFilePath, basicCS02XMLFilePath.Replace("_CS02.xml", "_Difference.txt"));
                    }
                }
            }


        }
    }
}
